﻿using System;
using FirebirdSql.Data.FirebirdClient;
using Csla;
using System.Collections;
using System.Collections.Generic;
using System.Reflection;
using System.Data;

namespace Model
{
    [Serializable()]
    public class LoaiTrangPhucBO : Csla.BusinessBase<LoaiTrangPhucBO>
    {
        #region Business Properties
        private int? _LoaiTrangPhucID = null;
        public int? LoaiTrangPhucID
        {
            get { return this._LoaiTrangPhucID; }
            set { this._LoaiTrangPhucID = value; PropertyHasChanged("LoaiTrangPhucID"); }
        }

        private string _Ten = null;
        public string Ten
        {
            get { return this._Ten; }
            set { this._Ten = value; PropertyHasChanged("Ten"); }
        }

        private string _MaSo = null;
        public string MaSo
        {
            get { return this._MaSo; }
            set { this._MaSo = value; PropertyHasChanged("MaSo"); }
        }

        private bool? _DELE = false;
        public bool? DELE
        {
            get { return this._DELE; }
            set { this._DELE = value; PropertyHasChanged("DELE"); }
        }

        private int? _STT0 = 0;
        public int? STT0
        {
            get { return _STT0; }
            set { _STT0 = value; }
        }

        protected override object GetIdValue()
        {
            return _LoaiTrangPhucID.Value;
        }
        #endregion Business Properties
        //==========================================================================

        #region Factory Methods
        public void SetIsNew()
        {
            MarkNew();
        }

        public void SetIsOld()
        {
            MarkOld();
        }

        public void SetIsDirty()
        {
            MarkDirty();
        }

        public object GetValue(string fieldName)
        {
            PropertyInfo propInfo = this.GetType().GetProperty(fieldName);
            return propInfo.GetValue(this, null);
        }

        public void SetValue(string fieldName, object value)
        {
            PropertyInfo propInfo = this.GetType().GetProperty(fieldName);
            propInfo.SetValue(this, value, null);
            MarkDirty();
        }

        private static int newLoaiTrangPhucID = -1;
        private LoaiTrangPhucBO()
        {
            _LoaiTrangPhucID = newLoaiTrangPhucID--;
        }

        public LoaiTrangPhucBO(FbDataReader dataReader)
        {
            MarkAsChild();
            Fetch(dataReader);
            MarkOld();
        }

        public LoaiTrangPhucBO(FbDataReader dataReader, bool isRoot)
        {
            if (!isRoot)
                MarkAsChild();
            Fetch(dataReader);
            MarkOld();
        }

        public static LoaiTrangPhucBO NewRootLoaiTrangPhucBO()
        {
            return DataPortal.Create<LoaiTrangPhucBO>(new RootCriteria());
        }

        internal static LoaiTrangPhucBO NewChildLoaiTrangPhucBO()
        {
            return DataPortal.Create<LoaiTrangPhucBO>(new ChildCriteria());
        }

        public static LoaiTrangPhucBO GetRootLoaiTrangPhucBO(int _LoaiTrangPhucID)
        {
            return DataPortal.Fetch<LoaiTrangPhucBO>(new RootCriteria(_LoaiTrangPhucID));
        }

        public static LoaiTrangPhucBO GetRootLoaiTrangPhucBOByWhere(string where)
        {
            return DataPortal.Fetch<LoaiTrangPhucBO>(where);
        }

        internal static LoaiTrangPhucBO GetChildLoaiTrangPhucBO(FbDataReader dataReader)
        {
            return new LoaiTrangPhucBO(dataReader);
        }

        public static void DeleteLoaiTrangPhucBO(int _LoaiTrangPhucID)
        {
            DataPortal.Delete(new RootCriteria(_LoaiTrangPhucID));
        }

        public static void DeleteLoaiTrangPhucBO(int _LoaiTrangPhucID, bool directDelete)
        {
            DataPortal.Delete(new RootCriteria(_LoaiTrangPhucID, directDelete));
        }
        #endregion Factory Methods
        //==========================================================================

        #region Criteria
        [Serializable()]
        public class RootCriteria
        {
            public int LoaiTrangPhucID;
            public bool directDelete = false;
            public RootCriteria(int _LoaiTrangPhucID)
            {
                this.LoaiTrangPhucID = _LoaiTrangPhucID;
                this.directDelete = false;
            }

            public RootCriteria(int _LoaiTrangPhucID, bool directDelete)
            {
                this.LoaiTrangPhucID = _LoaiTrangPhucID;
                this.directDelete = directDelete;
            }

            public RootCriteria() { }
        }

        [Serializable()]
        public class ChildCriteria
        {
            public int LoaiTrangPhucID;
            public bool directDelete = false;
            public ChildCriteria(int _LoaiTrangPhucID)
            {
                this.LoaiTrangPhucID = _LoaiTrangPhucID;
                this.directDelete = false;
            }

            public ChildCriteria(int _LoaiTrangPhucID, bool directDelete)
            {
                this.LoaiTrangPhucID = _LoaiTrangPhucID;
                this.directDelete = directDelete;
            }

            public ChildCriteria() { }
        }
        #endregion Criteria
        //==========================================================================

        #region Create
        [RunLocal]
        private void DataPortal_Create(RootCriteria criteria)
        {
        }

        private void DataPortal_Create(ChildCriteria criteria)
        {
            MarkAsChild();
            ValidationRules.CheckRules();
        }

        #endregion Create
        //==========================================================================

        #region Fetch
        [Transactional(TransactionalTypes.TransactionScope)]
        protected void DataPortal_Fetch(RootCriteria criteria)
        {
            this.Execute_Fetch(criteria);
        }

        protected void DataPortal_Fetch(string where)
        {
            this.Execute_Fetch(where);
        }

        protected void Execute_Fetch(RootCriteria criteria)
        {
            this.This_Fetch(criteria);
        }

        protected void Execute_Fetch(string where)
        {
            this.This_Fetch(where);
        }

        private void This_Fetch(RootCriteria criteria)
        {
            using (FbConnection conn = new FbConnection(string.Format(Session.CnnStr, Session.DB)))
            {
                conn.Open();
                using (FbCommand cmd = conn.CreateCommand())
                {
                    cmd.CommandText = "LOAITRANGPHUCBO_B";
                    cmd.CommandType = CommandType.StoredProcedure;
                    cmd.Parameters.Add("@WHEREEXP", FbDbType.VarChar).Value = "LOAITRANGPHUCID = " + criteria.LoaiTrangPhucID;

                    using (FbDataReader reader = cmd.ExecuteReader())
                    {
                        if (reader.Read())
                            Fetch(reader);
                    }
                }
            }
        }

        private void This_Fetch(string where)
        {
            using (FbConnection conn = new FbConnection(string.Format(Session.CnnStr, Session.DB)))
            {
                conn.Open();
                using (FbCommand cmd = conn.CreateCommand())
                {
                    cmd.CommandText = "LOAITRANGPHUCBO_B";
                    cmd.CommandType = CommandType.StoredProcedure;
                    cmd.Parameters.Add("@WHEREEXP", FbDbType.VarChar).Value = where;

                    using (FbDataReader reader = cmd.ExecuteReader())
                    {
                        if (reader.Read())
                            Fetch(reader);
                    }
                }
            }
        }

        private void Fetch(FbDataReader dataReader)
        {
            if (!(dataReader["LOAITRANGPHUCID"] is DBNull))
                this._LoaiTrangPhucID = (int)dataReader["LOAITRANGPHUCID"];
            if (!(dataReader["TEN"] is DBNull))
                this._Ten = ((string)dataReader["TEN"]).Trim();
            if (!(dataReader["MASO"] is DBNull))
                this._MaSo = ((string)dataReader["MASO"]).Trim();
            if (!(dataReader["DELE"] is DBNull))
                this._DELE = (((string)dataReader["DELE"]).Trim() == "1");
        }
        #endregion Fetch
        //==========================================================================

        #region Insert
        [Transactional(TransactionalTypes.TransactionScope)]
        protected override void DataPortal_Insert()
        {
            this.Execute_Insert();
        }

        protected void Execute_Insert()
        {
            this.This_Insert();
        }

        private void This_Insert()
        {
            using (FbConnection conn = new FbConnection(string.Format(Session.CnnStr, Session.DB)))
            {
                conn.Open();
                using (FbCommand cmd = conn.CreateCommand())
                {
                    cmd.CommandText = "LOAITRANGPHUCBO_I";
                    cmd.CommandType = CommandType.StoredProcedure;
                    cmd.Parameters.Add("@TEN", FbDbType.VarChar).Value = this._Ten;
                    cmd.Parameters.Add("@MASO", FbDbType.VarChar).Value = this._MaSo;
                    cmd.Parameters.Add("@DELE", FbDbType.Char).Value = (this._DELE.Value ? "1" : "0");

                    FbParameter pLoaiTrangPhucID = new FbParameter("@LOAITRANGPHUCID", FbDbType.Integer);
                    pLoaiTrangPhucID.Direction = ParameterDirection.ReturnValue;
                    cmd.Parameters.Add(pLoaiTrangPhucID);

                    cmd.ExecuteNonQuery();
                    this._LoaiTrangPhucID = (int)cmd.Parameters["@LOAITRANGPHUCID"].Value;
                }
            }
        }
        #endregion Insert
        //==========================================================================

        #region Update
        [Transactional(TransactionalTypes.TransactionScope)]
        protected override void DataPortal_Update()
        {
            this.Execute_Update();
        }

        protected void Execute_Update()
        {
            this.This_Update();
        }

        private void This_Update()
        {
            using (FbConnection conn = new FbConnection(string.Format(Session.CnnStr, Session.DB)))
            {
                conn.Open();
                using (FbCommand cmd = conn.CreateCommand())
                {
                    cmd.CommandText = "LOAITRANGPHUCBO_U";
                    cmd.CommandType = CommandType.StoredProcedure;
                    cmd.Parameters.Add("@LOAITRANGPHUCID", FbDbType.Integer).Value = this._LoaiTrangPhucID;
                    cmd.Parameters.Add("@TEN", FbDbType.VarChar).Value = this._Ten;
                    cmd.Parameters.Add("@MASO", FbDbType.VarChar).Value = this._MaSo;
                    cmd.Parameters.Add("@DELE", FbDbType.Char).Value = (this._DELE.Value ? "1" : "0");

                    cmd.ExecuteNonQuery();
                }
            }
        }
        #endregion Update
        //==========================================================================

        #region Delete
        [Transactional(TransactionalTypes.TransactionScope)]
        protected override void DataPortal_DeleteSelf()
        {
            DataPortal_Delete(new RootCriteria(_LoaiTrangPhucID.Value));
        }

        private void DataPortal_Delete(RootCriteria criteria)
        {
            this.Execute_Delete(criteria);
        }

        protected void Execute_Delete(RootCriteria criteria)
        {
            using (FbConnection conn = new FbConnection(string.Format(Session.CnnStr, Session.DB)))
            {
                conn.Open();
                using (FbCommand cmd = conn.CreateCommand())
                {
                    if (criteria.directDelete)
                    {
                        cmd.CommandText = "DELETE FROM LOAITRANGPHUC WHERE LOAITRANGPHUCID = " + criteria.LoaiTrangPhucID + ";";
                        cmd.ExecuteNonQuery();
                    }
                    else
                    {
                        cmd.CommandText = "UPDATE LOAITRANGPHUC SET LOAITRANGPHUC.DELE = '1' WHERE LOAITRANGPHUCID = " + criteria.LoaiTrangPhucID + ";";
                        cmd.ExecuteNonQuery();
                    }
                }
            }
        }
        #endregion Delete
        //==========================================================================

        #region Internal method
        internal void Insert()
        {
            if (!this.IsDirty) return;
            DataPortal_Insert();
            MarkOld();
        }

        internal void Update()
        {
            if (!this.IsDirty) return;
            DataPortal_Update();
            MarkOld();
        }

        internal void DeleteSelf()
        {
            if (!this.IsDirty) return;
            if (this.IsNew) return;
            DataPortal_Delete(new RootCriteria(_LoaiTrangPhucID.Value));
            MarkNew();
        }
        #endregion Internal method
        //==========================================================================
    }
}
